package com.example.yinhangmiaosha.interceptor;

import com.example.yinhangmiaosha.domain.User;
import com.example.yinhangmiaosha.filter.TimeFilter;
import com.example.yinhangmiaosha.service.TimeService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Component
public class TimeInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(TimeInterceptor.class);

    @Autowired
    private TimeService timeService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.info("TimeInterceptor拦截：[{}]", request.getRequestURL());
        if (handler instanceof HandlerMethod) {
            boolean isPermitted = timeService.getTimeIsPermitted();
            if (isPermitted == false) {
                LOGGER.error("请求拒绝，不在活动时间范围内！");
                response.sendRedirect("/nopermission");
                return false;
            }
        }
        return true;
    }
}
